package com.deep.test

import org.apache.spark.mllib.linalg._
import org.apache.spark.mllib.stat.Statistics
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.rdd.PairRDDFunctions

/**
 * @author sw
 * @create 2023-06-02 14:03
 */
object Test08 {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName("ALS")
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")

    val data = sc.makeRDD(Array(
      ("female", "Lily"),
      ("female", "Lucy"),
      ("female", "Emily"),
      ("female", "Kate"),
      ("female", "Alice"),
      ("male", "Tom"),
      ("male", "Roy"),
      ("male", "David"),
      ("male", "Frank"),
      ("male", "Jack")))

    val fractions: Map[String, Double] = Map("female" -> 0.6, "male" -> 0.4)
    val approxSample = data.sampleByKey(withReplacement = false, fractions, 1)
    approxSample.collect().
      foreach {
        println
      }

    val exactSample = data.sampleByKeyExact(withReplacement = false, fractions, 1)
    exactSample.collect().
      foreach {
        println
      }

  }
}
